package com.raymond.httpclient;

import java.util.concurrent.*;

/**
 * http请求的异步线程池管理
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-03-30 11:16
 */
public class HttpThreadPollManager {

    private static ThreadPoolExecutor poolExecutor = null;

    public static ThreadPoolExecutor getPoolExecutor() {
        if (poolExecutor == null) {
            synchronized (HttpThreadPollManager.class) {
                if (poolExecutor == null) {
                    poolExecutor = httpThreadExecutor(new ThreadPoolConfig());
                }
            }
        }
        return poolExecutor;
    }

    public static ThreadPoolExecutor getPoolExecutor(ThreadPoolConfig threadPollManager) {
        if (poolExecutor == null) {
            synchronized (HttpThreadPollManager.class) {
                if (poolExecutor == null) {
                    poolExecutor = httpThreadExecutor(threadPollManager);
                }
            }
        }
        return poolExecutor;
    }

    private static ThreadPoolExecutor httpThreadExecutor(ThreadPoolConfig threadPollManager) {
        return new ThreadPoolExecutor(threadPollManager.getCorePoolSize(), threadPollManager.getMaximumPoolSize(), threadPollManager.getKeepAliveTime(),
                threadPollManager.getUnit(), threadPollManager.getWorkQueue(), threadPollManager.getThreadFactory(), threadPollManager.getHandler());
    }
}
